package com.carleasoft.mps.adu.business.role.service.impl;

import com.carleasoft.mps.adu.business.role.bean.vo.AuthorityMenuPermsVo;
import com.carleasoft.mps.adu.business.menu.dao.AuthorityMenuDao;
import com.carleasoft.mps.adu.business.role.dao.AuthorityPermMenuDao;
import com.carleasoft.mps.adu.business.role.service.AuthorityPermMenuService;
import com.carleasoft.mps.core.user.Constants;
import com.carleasoft.mps.core.tree.DataBizTreeVo;
import com.carleasoft.mps.core.tree.TreeBuild;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Description: role serviceImpl</p>
 *
 * @author ztm
 * @since 2020-05-11
 */
@Service
public class AuthorityPermMenuServiceImpl implements AuthorityPermMenuService {
    @Autowired
    AuthorityPermMenuDao authorityPermMenuDao;

    /**
     * 菜单权限查询封装
     *
     * @param roleCodes
     * @return AuthorityRoleVo
     */
    public AuthorityMenuPermsVo getMenuPermByRoleCode(List<String> roleCodes, Integer isMarking) {
        if (roleCodes == null || roleCodes.size() == 0) {
            UserCacheData userCacheData = ThreadUserContext.getCurrentCacheUser();
            if (userCacheData.getUserCat() != Constants.SUPER_ADMIN) {
                //返回为空，避免使用的时候空指针
                AuthorityMenuPermsVo authorityMenuPermsVo = new AuthorityMenuPermsVo();
                return authorityMenuPermsVo;
            }
        }
        //根据菜单code获取对应的菜单实体对象
        AuthorityMenuPermsVo authorityMenuPermsVo = new AuthorityMenuPermsVo();
        Boolean isOk = true;
        List<String> webmenuCodesList = authorityPermMenuDao.selectMenusCodeByRoleCodes(roleCodes, isMarking, Constants.WEB, isOk);
        if (webmenuCodesList != null && webmenuCodesList.size() > 0) {
            authorityMenuPermsVo.setWebResourcesList(webmenuCodesList);
            List<DataBizTreeVo> authorityMenuEntitiesWeb = authorityPermMenuDao.selectMenusByMenuCodes(webmenuCodesList, isMarking, Constants.WEB);
            if (authorityMenuEntitiesWeb != null && authorityMenuEntitiesWeb.size() > 0)
                authorityMenuPermsVo.setWebResourcesTree(this.resolve(authorityMenuEntitiesWeb));
        }

        List<String> appmenuCodesList = authorityPermMenuDao.selectMenusCodeByRoleCodes(roleCodes, isMarking, Constants.APP, isOk);
        if (appmenuCodesList != null && appmenuCodesList.size() > 0) {
            authorityMenuPermsVo.setAppResourcesList(appmenuCodesList);
            List<DataBizTreeVo> authorityMenuEntitiesApp = authorityPermMenuDao.selectMenusByMenuCodes(appmenuCodesList, isMarking, Constants.APP);
            if (authorityMenuEntitiesApp != null && authorityMenuEntitiesApp.size() > 0)
                authorityMenuPermsVo.setAppResourcesTree(this.resolve(authorityMenuEntitiesApp));
        }
        return authorityMenuPermsVo;
    }

    /**
     * 菜单权限code查询
     *
     * @param roleCodes
     * @return AuthorityRoleVo
     */
    public AuthorityMenuPermsVo getMenuPermListByRoleCode(List<String> roleCodes, Integer isMarking, boolean isQueryHide) {
        if (roleCodes == null || roleCodes.size() == 0) {
            UserCacheData userCacheData = ThreadUserContext.getCurrentCacheUser();
            if (userCacheData.getUserCat() != Constants.SUPER_ADMIN) {
                //返回为空，避免使用的时候空指针
                AuthorityMenuPermsVo authorityMenuPermsVo = new AuthorityMenuPermsVo();
                return authorityMenuPermsVo;
            }
        }
        //根据菜单code获取对应的菜单实体对象
        AuthorityMenuPermsVo authorityMenuPermsVo = new AuthorityMenuPermsVo();

        List<String> webmenuCodesList = authorityPermMenuDao.selectMenusCodeByRoleCodes(roleCodes, isMarking, Constants.WEB, isQueryHide);
        if (webmenuCodesList != null && webmenuCodesList.size() > 0) {
            authorityMenuPermsVo.setWebResourcesList(webmenuCodesList);
        }

        List<String> appmenuCodesList = authorityPermMenuDao.selectMenusCodeByRoleCodes(roleCodes, isMarking, Constants.APP, isQueryHide);
        if (appmenuCodesList != null && appmenuCodesList.size() > 0) {
            authorityMenuPermsVo.setAppResourcesList(appmenuCodesList);
        }
        return authorityMenuPermsVo;
    }

    /**
     * 解析菜单
     *
     * @param authorityMenuEntities
     */
    private List<DataBizTreeVo> resolve(List<DataBizTreeVo> authorityMenuEntities) {
        List<DataBizTreeVo> DataBizTreeVos = TreeBuild.getTreeList(Constants.PARENT_CODE, authorityMenuEntities);
        return DataBizTreeVos;
    }
}
